# Maintainer: AndyRTR <andyrtr@archlinux.org>

pkgbase=hunspell-en
pkgname=('hunspell-en_us' 'hunspell-en_au' 'hunspell-en_ca' 'hunspell-en_gb')
pkgver=2020.12.07
pkgrel=3
pkgdesc="English hunspell dictionaries"
arch=(any)
url="http://wordlist.aspell.net/dicts/"
license=('LGPL' 'custom:scowl')
makedepends=('qt5-webengine')
optdepends=('hunspell: the spell checking libraries and apps')
source=(https://downloads.sourceforge.net/project/wordlist/speller/$pkgver/hunspell-en_US-large-$pkgver.zip
        https://downloads.sourceforge.net/project/wordlist/speller/$pkgver/hunspell-en_AU-large-$pkgver.zip
        https://downloads.sourceforge.net/project/wordlist/speller/$pkgver/hunspell-en_CA-large-$pkgver.zip
        https://downloads.sourceforge.net/project/wordlist/speller/$pkgver/hunspell-en_GB-large-$pkgver.zip
        hunspell-en-allow-non-typographical.marks.patch)
sha512sums=('b370fc77735666d9de1c0cc9a1028819974b254291a0aacbcf6037a8cbb23f75538f2130249a0bab46d32dbbd3e0788407c227423d375ef1430dff7a016092dc'
            '020684f5d8abf227a6566ebfe7343ee51f7f79576a89e9955ca614b3847e2b7e7be48025b22265a4cb8f86e5e8cbb2620385b46104c3ffec5c604775dea0bec0'
            'a3c591d8593b3fb6d0c28c2764cb85dec6650073f7b6a1074e9d6f96cbb2194d4bd0cbd61be59ebe14f89e3f5d5d83f10706e69130ec5c7d90f088005de9b464'
            'a402c40717cd185ea8c14be2d4dc7df67580f197d276ae660a7cf478ae3e07e469c78271feb0c4c6b7c1c418bfb10e7f42c1e5f4b9635a4d3c47e1af4a747a4c'
            '12571c9a64bb205a30a4843b88c16373b093c92506612abc7eaae7ee2c3694a3eaa98f5b236cde4a185e442ea8dfe2e88da0676527c43a8b55714562e49266a1')
b2sums=('f7ab7abb438806826408a5641b82094a69dc85c403389e6ea63d726c68dee49542e1b826f73261eef37cb53f291047d95b91011c501ee00e0e7fd62e301850d3'
        '91d7a7c925d24c27591e70df8f7db9dca469106fcb14dbeb8befb645befe345237b76d7f0f38027b2cb4c894ea53c2574c938341d890411d2648519dd9943c93'
        'f2b6b9ece44c9c07f084d8af0deec96218390288ec7caf07316ef517042f4a3b0d581353d1635d4e2a2797867ed95a6e76325c42e3364a15db8a49a94f50ddff'
        'f71b304e97e10368180d0abb7b2304649c4e481848226d693768f54dca96b9f4f1e85e54efd052546236cbc94b5caba19df801d75c46ab20a68df98e7758aeb1'
        'e0ef68e17c5530fa7c8c9de9e8977ff92f4a28df55cb6be3ba4a4b3c3fef415d61cf3467633322ba1ff28554775d012e186b9686f8114f33bdbac4d46c322f60')

prepare() {
  sed -i "s:SET UTF8:SET UTF-8:g" *.aff

  # recognize apostrophes - FS#70425
  # https://sourceforge.net/p/hunspell/patches/35/
  patch -Np1 -i ./hunspell-en-allow-non-typographical.marks.patch
}

package_hunspell-en_us() {
  pkgdesc="US English hunspell dictionaries"
  # TODO: remove conflicts/provides/replaces 2021-09-23
  conflicts=('hunspell-en_US')
  provides=('hunspell-en_US')
  replaces=('hunspell-en_US<=2020.12.07-1')

  cd "$srcdir"
  install -vDm 644 en_US*.dic en_US*.aff -t "$pkgdir/usr/share/hunspell"

  pushd "$pkgdir/usr/share/hunspell/"
    en_US_aliases="en_US"
    for lang in $en_US_aliases; do
      ln -s en_US-large.aff $lang.aff
      ln -s en_US-large.dic $lang.dic
    done
  popd

  # Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
    _filename="$(basename "$_file")"
     qwebengine_convert_dict $_file "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  # myspell symlinks
  install -dm755 "${pkgdir}/usr/share/myspell/dicts"
  pushd "$pkgdir/usr/share/myspell/dicts"
    for file in "$pkgdir/usr/share/hunspell/"*; do
      ln -sv /usr/share/hunspell/$(basename $file) .
    done
  popd

  # licenses
  install -D -m644 README_en_US-large.txt "$pkgdir/usr/share/licenses/$pkgname/Copyright_en_US-large"
}

package_hunspell-en_au() {
  pkgdesc="AU English hunspell dictionaries"
  # TODO: remove conflicts/provides/replaces 2021-09-23
  conflicts=('hunspell-en_AU')
  provides=('hunspell-en_AU')
  replaces=('hunspell-en_AU<=2020.12.07-1')

  cd "$srcdir"
  install -vDm 644 en_AU*.dic en_AU*.aff -t "$pkgdir/usr/share/hunspell"

  pushd "$pkgdir/usr/share/hunspell/"
    en_AU_aliases="en_AU"
    for lang in $en_AU_aliases; do
      ln -s en_AU-large.aff $lang.aff
      ln -s en_AU-large.dic $lang.dic
    done
  popd

  # Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
    _filename="$(basename "$_file")"
     qwebengine_convert_dict $_file "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  # myspell symlinks
  install -dm755 "${pkgdir}/usr/share/myspell/dicts"
  pushd "$pkgdir/usr/share/myspell/dicts"
    for file in "$pkgdir/usr/share/hunspell/"*; do
      ln -sv "/usr/share/hunspell/$(basename "$file")" .
    done
  popd

  # licenses
  install -D -m644 README_en_AU-large.txt "$pkgdir/usr/share/licenses/$pkgname/Copyright_en_AU-large"
}

package_hunspell-en_ca() {
  pkgdesc="CA English hunspell dictionaries"
  # TODO: remove conflicts/provides/replaces 2021-09-23
  conflicts=('hunspell-en_CA')
  provides=('hunspell-en_CA')
  replaces=('hunspell-en_CA<=2020.12.07-1')

  cd "$srcdir"
  install -vDm 644 en_CA*.dic en_CA*.aff -t "$pkgdir/usr/share/hunspell"

  pushd "$pkgdir/usr/share/hunspell/"
    en_CA_aliases="en_CA"
    for lang in $en_CA_aliases; do
      ln -s en_CA-large.aff $lang.aff
      ln -s en_CA-large.dic $lang.dic
    done
  popd

  # Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
    _filename="$(basename "$_file")"
     qwebengine_convert_dict "$_file" "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  # myspell symlinks
  install -dm755 "${pkgdir}/usr/share/myspell/dicts"
  pushd "$pkgdir/usr/share/myspell/dicts"
    for file in "$pkgdir/usr/share/hunspell/"*; do
      ln -sv "/usr/share/hunspell/$(basename $file)" .
    done
  popd

  # licenses
  install -D -m644 README_en_CA-large.txt "$pkgdir/usr/share/licenses/$pkgname/Copyright_en_CA-large"
}

package_hunspell-en_gb() {
  pkgdesc="GB English hunspell dictionaries"
  # TODO: remove conflicts/provides/replaces 2021-09-23
  conflicts=('hunspell-en_GB')
  provides=('hunspell-en_GB')
  replaces=('hunspell-en_GB<=2020.12.07-1')

  cd "$srcdir"
  install -vDm 644 en_GB*.dic en_GB*.aff -t "$pkgdir/usr/share/hunspell"

  pushd "$pkgdir/usr/share/hunspell/"
    en_GB_aliases="en_AG en_BS en_BW en_BZ en_DK en_GB en_GH en_HK en_IE en_IN en_JM en_NA en_NG en_NZ en_SG en_TT en_ZA en_ZW"
    for lang in $en_GB_aliases; do
      ln -s en_GB-large.aff $lang.aff
      ln -s en_GB-large.dic $lang.dic
    done
  popd

  # Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
    _filename="$(basename "$_file")"
     qwebengine_convert_dict "$_file" "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  # myspell symlinks
  install -dm755 "${pkgdir}/usr/share/myspell/dicts"
  pushd "$pkgdir/usr/share/myspell/dicts"
    for file in "$pkgdir/usr/share/hunspell/"*; do
      ln -sv "/usr/share/hunspell/$(basename "$file")" .
    done
  popd

  # licenses
  install -D -m644 README_en_GB-large.txt "$pkgdir/usr/share/licenses/$pkgname/Copyright_en_GB-large"
}
